package com.zhongwang.business.module.estimateBudgetAccountingManage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.gen.entity.BusBudgetAccountingBasicInfoConfig;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusBudgetAccountingTask;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.*;
import com.zhongwang.business.module.estimateBudgetAccountingManage.service.AccountingService;
import com.zhongwang.business.module.estimateBudgetAccountingManage.service.GetDataService;
import com.zhongwang.business.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 徐一贺
 * @version 1.0
 * @exception: AccountingController 核算控制层
 * @date 2020/9/15 8:22
 */
@Controller
@RequestMapping("/accountManage")
public class AccountingController extends BaseController {
    @Autowired
    private AccountingService accountingService;
    @Autowired
    private GetDataService getDataService;

    /**
     * page 跳转页面
     *
     * @param model 页面渲染模型
     * @return java.lang.String
     * @author 徐一贺
     * @date 2020/9/15 8:26
     * @version 1.0
     */
    @GetMapping
    @RequiresPermissions("ver:list:view")
    public String page(Model model) {
        // 获取工作组列表
        List<BusinessGroupModel> groupModelList = accountingService.findBusinessGroupList();
        model.addAttribute("groupModelList", groupModelList);
        return "accountManage/list/page";
    }
    /**  queryPage 核算成本清单查询
     * @param model
     * @return java.lang.String
     * @author 徐一贺
     * @date 2021/1/8 12:42
     */
    @GetMapping("/queryPage")
    @RequiresPermissions("ver:query:vew")
    public String queryPage(Model model) {
        // 获取工作组列表
        List<BusinessGroupModel> groupModelList = accountingService.findBusinessGroupList();
        model.addAttribute("groupModelList", groupModelList);
        return "accountManage/query/page";
    }

    /**
     * submitList 跳转到核算成本提交列表
     *
     * @return java.lang.String
     * @author 徐一贺
     * @date 2020/11/10 20:08
     * @version 1.0
     */
    @GetMapping("/submitList")
    @RequiresPermissions("ver:submit:view")
    public String submitList() {
        return "accountManage/submit/page";
    }

    /**
     * submitList 跳转到核算成本审核列表
     *
     * @return java.lang.String
     * @author 徐一贺
     * @date 2020/11/10 20:08
     * @version 1.0
     */
    @GetMapping("/approvalList")
    @RequiresPermissions("ver:audit:audit")
    public String approvalList() {
        return "accountManage/approval/page";
    }

    /**
     * accountingSubmit 核算清单提交
     *
     * @param id 核算子集单据表主键
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/24 15:54
     */
    @ResponseBody
    @PostMapping("/accountingSubmit")
    @RequiresPermissions("ver:submit:commit")
    public JsonResult accountingSubmit(Integer id) {
        return accountingService.accountingSubmit(id);
    }

    /**
     * accountingSubmit 概算清单提交
     *
     * @param id 核算子集单据表主键
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 20:27:04
     */
    @ResponseBody
    @PostMapping("/estimatingSubmit")
    @RequiresPermissions("rou:submit:commit")
    public JsonResult estimatingSubmit(Integer id) {
        return accountingService.estimatingSubmit(id);
    }

    /**
     * findApprovalList 获取核算成本清单提审核表
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/25 8:47
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/findApprovalList")
    public JsonResult findApprovalList(AccountingProjectModel param) {
        startPage();
        return jsonResult(accountingService.findApprovalList(param));
    }

    /**
     * startAccounting 开始核算
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/26 18:40
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/startAccounting")
    public JsonResult startAccounting(BusBudgetAccountingTaskModel param) {
        return accountingService.startAccounting(param);
    }

    /**
     * list 获取待核算项目列表
     *
     * @param param 检索条件
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/9/15 9:40
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(AccountingProjectModel param) {
        startPage();
        return jsonResult(accountingService.list(param));
    }

    /**
     * queryList 查询列表
     *
     * @param param 查询参数
     * @return com.zhongwang.business.common.JsonResult
     * @throws
     * @author 徐一贺
     * @date 2020/11/30 20:05
     */
    @ResponseBody
    @PostMapping("/queryList")
    public JsonResult queryList(AccountingProjectModel param) {
        startPage();
        return jsonResult(accountingService.queryList(param));
    }

    @ResponseBody
    @PostMapping("/getParameters")
    /**
     * getParameters 依据单据表主键获取参数列表
     *
     * @param batId 单据表主键
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/7 10:16
     * @version 1.0
     */
    public JsonResult getParameters(Integer batId) {
        List<BusBudgetAccountingBasicInfoConfig> list =
                accountingService.findBusBudgetAccountingBasicInfoConfigList(batId);
        return success().setData(list);
    }

    @ResponseBody
    @PostMapping("/saveParameters")
    /**
     * saveParameters 保存参数
     *
     * @param params 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/7 12:36
     * @version 1.0
     */
    public JsonResult saveParameters(String jsonParams, Integer batId) {
        JSONArray params = JSON.parseArray(jsonParams);
        return accountingService.saveParameters(params, batId);
    }

    @ResponseBody
    @PostMapping("/saveItemTexture")
    /**
     * saveItemTexture 保存材质
     *
     * @param params 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/8 13:41
     * @version 1.0
     */
    public JsonResult saveItemTexture(BusBudgetAccountingCost params) {
        return accountingService.saveItemTexture(params);
    }

    /**
     * regenerateCostSheet 重新生成核算清单
     *
     * @param params 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/31 15:00
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/regenerateCostSheet")
    public JsonResult regenerateCostSheet(BusBudgetAccountingCost params) {
        return accountingService.regenerateCostSheet(params);
    }

    /**
     * getCostInfo 获取清单信息
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/27 16:11
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/getCostInfo")
    public JsonResult getCostInfo(BusBudgetAccountingCostModel param) {
        return accountingService.getCostInfo(param);
    }

    /**
     * saveCostItem 保存成本项数据
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/10/27 19:37
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/saveCostItem")
    public JsonResult saveCostItem(BusBudgetAccountingCostModel param) {
        return accountingService.saveCostItem(param);
    }

    /**
     * saveAccountingCost 保存核算清单
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/10 18:39
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/saveAccountingCost")
    public JsonResult saveAccountingCost(BusBudgetAccountingTaskModel param) {
        return accountingService.saveAccountingCost(param);
    }

    /**
     * saveEstimatingCost 保存概算
     *
     * @param param
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2021/1/7 20:12
     */
    @ResponseBody
    @PostMapping("/saveEstimatingCost")
    public JsonResult saveEstimatingCost(BusBudgetAccountingTaskModel param) {
        return accountingService.saveEstimatingCost(param);
    }

    /**
     * saveMainParameter 保存主要参数
     *
     * @param param 主要参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/12/10 10:25
     */
    @ResponseBody
    @PostMapping("/saveMainParameter")
    public JsonResult saveMainParameter(BusBudgetAccountingTaskModel param) {
        return accountingService.saveMainParameter(param);
    }

    /**
     * showAccCostSheet 跳转到成本项查看页
     *
     * @param batId 单据表主键
     * @param model 渲染模型
     * @return java.lang.String
     * @throws
     * @author 徐一贺
     * @date 2020/11/23 14:51
     */
    @GetMapping("/showAccCostSheet/{batId}")
    public String showAccCostSheet(@PathVariable("batId") Integer batId, Model model) {
        BusBudgetAccountingTask order = accountingService.getBusBudgetAccountingTaskByPrimaryKey(batId);
        List<BusBudgetAccountingCost> costList =
                accountingService.findShowBusBudgetAccountingCostByBatId(batId);
        // 基础参数数据
        List<BusBudgetAccountingBasicInfoConfig> busBudgetAccountingBasicInfoConfigList =
                getDataService.findBusBudgetAccountingBasicInfoConfigByBatId(batId);
        // 获取核算设备总重
        BigDecimal equipmentTotalWeight =
                getDataService.getAccountingEquipmentTotalWeightByBatId(batId);
        // 获取核算设备净重
        BigDecimal equipmentTotalNetWeight =
                getDataService.getAccountingEquipmentTotalNetWeightByBatId(batId);
        ProjectModel project = getDataService.getProjectModel(order.getProCode());
        model.addAttribute("project", project);
        model.addAttribute("busBudgetAccountingCosts", costList);
        model.addAttribute(
                "busBudgetAccountingBasicInfoConfigList", busBudgetAccountingBasicInfoConfigList);
        model.addAttribute("equipmentTotalWeight", equipmentTotalWeight);
        model.addAttribute("equipmentTotalNetWeight", equipmentTotalNetWeight);
        return "accountManage/cost/showAccCostSheet/page";
    }

    /**
     * showAccCostSheet 跳转到成本项审核查看页
     *
     * @param batId 单据表主键
     * @param model 渲染模型
     * @return java.lang.String
     * @throws
     * @author 徐一贺
     * @date 2020/11/23 14:51
     */
    @GetMapping("/showAccCostSheetByApproval/{batId}/{mode}")
    @RequiresPermissions(value={"ver:audit:check","ver:submit:view"},logical = Logical.OR)
    public String showAccCostSheetByApproval(
            @PathVariable("batId") Integer batId, @PathVariable("mode") String mode, Model model) {
        BusBudgetAccountingTaskModel order =
                accountingService.getBusBudgetAccountingTaskAndAppInfoByPrimaryKey(batId);
        // 获取驳回列表数据
        List<AccountingProjectModel> rejectList = accountingService.findVerifyProcessByBatId(batId);
        AccountingProjectModel accountingProjectModel1 =
                accountingService.getVerifyProcessId(batId, ShiroUtils.getUserId());
        if (accountingProjectModel1 != null) {
            order.setVerifyProcessId(
                    accountingService.getVerifyProcessId(batId, ShiroUtils.getUserId()).getId());
        }
        ProjectModel project = getDataService.getProjectModel(order.getProCode());

        List<BusBudgetAccountingCost> costList =
                accountingService.findShowBusBudgetAccountingCostByBatId(batId);
        // 基础参数数据
        List<BusBudgetAccountingBasicInfoConfig> busBudgetAccountingBasicInfoConfigList =
                getDataService.findBusBudgetAccountingBasicInfoConfigByBatId(batId);
        // 获取核算设备总重
        BigDecimal equipmentTotalWeight =
                getDataService.getAccountingEquipmentTotalWeightByBatId(batId).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
        // 获取核算设备净重
        BigDecimal equipmentTotalNetWeight =
                getDataService.getAccountingEquipmentTotalNetWeightByBatId(batId).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
        model.addAttribute("rejectList", rejectList);
        model.addAttribute("order", order);
        model.addAttribute("project", project);
        model.addAttribute("mode", mode);
        model.addAttribute("busBudgetAccountingCosts", costList);
        model.addAttribute(
                "busBudgetAccountingBasicInfoConfigList", busBudgetAccountingBasicInfoConfigList);
        model.addAttribute("equipmentTotalWeight", equipmentTotalWeight);
        model.addAttribute("equipmentTotalNetWeight", equipmentTotalNetWeight);
        return "accountManage/approval/costSheet/page";
    }

    @GetMapping("/showEstCostSheetByApproval/{batId}/{mode}")
    @RequiresPermissions("rou:audit:check")
    public String showEstCostSheetByApproval(
            @PathVariable("batId") Integer batId, @PathVariable("mode") String mode, Model model) {
        BusBudgetAccountingTaskModel order =
                accountingService.getBusBudgetAccountingTaskAndAppInfoByPrimaryKey(batId);
        // 获取驳回列表数据
        List<AccountingProjectModel> rejectList = accountingService.findVerifyProcessByBatId(batId);
        AccountingProjectModel accountingProjectModel1 =
                accountingService.getVerifyProcessId(batId, ShiroUtils.getUserId());
        if (accountingProjectModel1 != null) {
            order.setVerifyProcessId(
                    accountingService.getVerifyProcessId(batId, ShiroUtils.getUserId()).getId());
        }
        ProjectModel project = getDataService.getProjectModel(order.getProCode());

        List<BusBudgetAccountingCost> costList =
                accountingService.findShowBusBudgetAccountingCostByBatId(batId);
        // 基础参数数据
        List<BusBudgetAccountingBasicInfoConfig> busBudgetAccountingBasicInfoConfigList =
                getDataService.findBusBudgetAccountingBasicInfoConfigByBatId(batId);
        // 获取核算设备总重
        BigDecimal equipmentTotalWeight =
                getDataService.getAccountingEquipmentTotalWeightByBatId(batId).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
        // 获取核算设备净重
        BigDecimal equipmentTotalNetWeight =
                getDataService.getAccountingEquipmentTotalNetWeightByBatId(batId).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
        model.addAttribute("rejectList", rejectList);
        model.addAttribute("order", order);
        model.addAttribute("project", project);
        model.addAttribute("mode", mode);
        model.addAttribute("busBudgetAccountingCosts", costList);
        model.addAttribute(
                "busBudgetAccountingBasicInfoConfigList", busBudgetAccountingBasicInfoConfigList);
        model.addAttribute("equipmentTotalWeight", equipmentTotalWeight);
        model.addAttribute("equipmentTotalNetWeight", equipmentTotalNetWeight);
        return "estimateManage/approval/costSheet/page";
    }

    /**
     * showAccCostSheet 跳转到成本项审核查看页
     *
     * @param batId 单据表主键
     * @param model 渲染模型
     * @return java.lang.String
     * @throws
     * @author 徐一贺
     * @date 2020/11/23 14:51
     */
    @GetMapping("/showAccCostSheetByApproval/{batId}/{id}/{mode}")
    @RequiresPermissions("ver:audit:check")
    public String showAccCostSheetByApproval(
            @PathVariable("batId") Integer batId,
            @PathVariable("id") Integer id,
            @PathVariable("mode") String mode,
            Model model) {
        BusBudgetAccountingTaskModel order =
                accountingService.getBusBudgetAccountingTaskAndAppInfoByPrimaryKey(batId);
        // 获取驳回列表数据
        List<AccountingProjectModel> rejectList = accountingService.findVerifyProcessByBatId(batId);
        AccountingProjectModel accountingProjectModel1 =
                accountingService.getVerifyProcessId(batId, ShiroUtils.getUserId());
        if (accountingProjectModel1 != null) {
            order.setVerifyProcessId(
                    accountingService.getVerifyProcessId(batId, ShiroUtils.getUserId()).getId());
        }
        ProjectModel project = getDataService.getProjectModel(order.getProCode());
        List<BusBudgetAccountingCost> costList =
                accountingService.findShowBusBudgetAccountingCostByBatId(batId);
        // 基础参数数据
        List<BusBudgetAccountingBasicInfoConfig> busBudgetAccountingBasicInfoConfigList =
                getDataService.findBusBudgetAccountingBasicInfoConfigByBatId(batId);
        // 获取核算设备总重
        BigDecimal equipmentTotalWeight =
                getDataService.getAccountingEquipmentTotalWeightByBatId(batId).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
        // 获取核算设备净重
        BigDecimal equipmentTotalNetWeight =
                getDataService.getAccountingEquipmentTotalNetWeightByBatId(batId).divide(new BigDecimal(project.getNum()), 2, BigDecimal.ROUND_HALF_UP);
        model.addAttribute("rejectList", rejectList);
        model.addAttribute("order", order);
        model.addAttribute("project", project);
        model.addAttribute("mode", mode);
        model.addAttribute("busBudgetAccountingCosts", costList);
        model.addAttribute(
                "busBudgetAccountingBasicInfoConfigList", busBudgetAccountingBasicInfoConfigList);
        model.addAttribute("equipmentTotalWeight", equipmentTotalWeight);
        model.addAttribute("equipmentTotalNetWeight", equipmentTotalNetWeight);
        return "accountManage/approval/costSheet/page";
    }

    /**
     * accountingAdopt 核算审批 通过 或 驳回
     *
     * @param param 参数
     * @return jcom.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/25 15:51
     */
    @ResponseBody
    @PostMapping("/accountingApproval")
    public JsonResult accountingApproval(AccountingProjectModel param) {
        return getDataService.accountingApproval(param);
    }

    /**
     * findSubmitList 获取核算成本清单提交列表
     *
     * @param param 参数
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/11/10 20:14
     * @version 1.0
     */
    @ResponseBody
    @PostMapping("/findSubmitList")
    public JsonResult findSubmitList(AccountingProjectModel param) {
        startPage();
        return jsonResult(accountingService.findSubmitList(param));
    }

    /**
     * getNotHaveTaxTotalAmount 根据单据表主键，获取去税的总金额。总金额 = 人工 + 去税后的辅材金额。
     *
     * @param condition 单据表主键
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2020/12/14 15:27
     */
    @ResponseBody
    @PostMapping("/getNotHaveTaxTotalAmount")
    public JsonResult getNotHaveTaxTotalAmount(BusBudgetAccountingBasicInfoConfigModel condition) {
        return accountingService.getNotHaveTaxTotalAmount(condition);
    }

    /**
     * changeOrder 拖动改变顺序
     *
     * @param dragItemId
     * @param prevItemId
     * @return com.zhongwang.business.common.JsonResult
     * @author 徐一贺
     * @date 2021/1/7 15:36
     */
    @ResponseBody
    @PostMapping("/changeOrder")
    public JsonResult changeOrder(@RequestParam("dragItemId") Integer dragItemId, @RequestParam("prevItemId") Integer prevItemId) {
        return accountingService.changeOrder(dragItemId, prevItemId);
    }
    /**
     * @methodName: exportCostSheet
     * @description: 导出成本清单
     * @param: response
     * @param: param
     * @return： void
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣，盖世奇
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/7 14:47
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    @ResponseBody
    @GetMapping("/exportCostSheet")
    public void exportCostSheet(HttpServletResponse response, BusBudgetAccountingTaskModel param) {
        accountingService.exportCostSheet(response,param);
    }
}
